<?php
namespace Common\Controller;
use Think\Controller;
class UserController extends Controller {
	public function _initialize(){
        $this->assign('index',U('Index/index'));
		$this->assign('login',U('Index/login'));
    	$this->assign('regist',U('Index/regist'));
        $this->assign('loginout',U('Index/loginout'));
        $this->assign('product',U('Index/product'));
    	$this->assign('user',U('Consumer/user_center'));
    	$this->assign('address',U('Consumer/user_address'));
        $this->assign('shou',U('Consumer/user_collect'));
        $this->assign('orderlist',U('Order/order_list'));
        $this->assign('obligation',U('Order/obligations'));
        $this->assign('shipments',U('Order/shipment'));
        $this->assign('receiveds',U('Order/received'));
    	$this->assign('cart',U('Cart/cart'));
    	$this->assign('addcart',U('Cart/addcart'));
        $this->assign('order',U('Cart/order_payment'));
    	$this->assign('pay',U('Cart/order'));
        $this->assign('delete',U('Cart/delete'));
        
    	    	//分类导航
    	$list=$this->getCateByPid();
    	$this->assign('list',$list);

    	$cart=$this->getcart();
        $sumtotal=0;
        foreach ($cart as $key => $value) {
            $num=M('cart')->where('comuser_id='.session('id'))->count();
            $this->assign('num',$num);
            $sumtotal+=$value['price']*$value['number'];
            $this->assign('sumtotal',$sumtotal);
        }
        $this->assign('buycart',$cart);
        
        if (session('id')) {
            $orderModel=M('order');
            $obligations=$orderModel->where('comuser_id='.session('id'). ' and status=1')->count();
            $shipment=$orderModel->where('comuser_id='.session('id'). ' and status=2')->count();
            $received=$orderModel->where('comuser_id='.session('id'). ' and status=3')->count();
            $this->assign('obligations',$obligations);
            $this->assign('shipment',$shipment);
            $this->assign('received',$received);
        } 
	 }
	public function getCateByPid($pid=0){
	    $CategoryModel = M('Category');
	    $result = $CategoryModel->where('pid='.$pid.' and status=1')->select();
	    
	    if(!empty($result)){
	        foreach ($result as $key => $value) {
	            $result[$key]['child']=$this->getCateByPid($value['id']);
	        }
	    }
	    return $result;
	}
	public function getcart(){
        if (session('id')) {
            $result=M('cart')->field('fm_cart.*,fm_merchandise.merchandise,fm_merchandise.price,(select fm_photos.photos from fm_photos where fm_merchandise.id=fm_photos.merchandise_id limit 1) as photos')->join('left join fm_merchandise on fm_merchandise.id=fm_cart.merchandise_id')->where('comuser_id='.session('id'))->select(); 
        }else{
            $result=array();
        } 
     return $result;
    }
	
}